package cs757;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class DlgAddProfessor extends JDialog {

	private static final long serialVersionUID = 9072760272680385942L;
	
	private final JPanel contentPanel = new JPanel();
	private JLabel lblSsn;
	private JTextField textFieldSsnFirst;
	private JTextField textFieldSsnMiddle;
	private JTextField textFieldSsnLast;
	private JTextField textFieldSpecialty;
	private JTextField textFieldRank;
	private JTextField textFieldAge;
	private JTextField textFieldName;

	//Database connection
	private DatabaseConnector conn;
	
	private boolean ok = false;
	private JComboBox comboBoxDept;
	private JTextField textFieldPctTime;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgAddProfessor dialog = new DlgAddProfessor();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DlgAddProfessor() {
		setTitle("Add Professor");
		setBounds(100, 100, 367, 419);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		{
			lblSsn = new JLabel("SSN:");
		}
		textFieldSsnFirst = new JTextField();
		textFieldSsnFirst.setColumns(10);
		JLabel label = new JLabel("-");
		textFieldSsnMiddle = new JTextField();
		textFieldSsnMiddle.setColumns(10);
		JLabel label_1 = new JLabel("-");
		textFieldSsnLast = new JTextField();
		textFieldSsnLast.setColumns(10);
		JLabel lblName = new JLabel("Name:");
		JLabel lblAge = new JLabel("Age:");
		JLabel lblRank = new JLabel("Rank:");
		JLabel lblSpecialty = new JLabel("Specialty:");
		textFieldSpecialty = new JTextField();
		textFieldSpecialty.setColumns(10);
		textFieldRank = new JTextField();
		textFieldRank.setColumns(10);
		textFieldAge = new JTextField();
		textFieldAge.setColumns(10);
		textFieldName = new JTextField();
		textFieldName.setColumns(10);
		
		JLabel lblinteger = new JLabel("(integer)");
		
		comboBoxDept = new JComboBox();
		
		JLabel labelDept = new JLabel("Dept:");
		
		textFieldPctTime = new JTextField();
		textFieldPctTime.setColumns(10);
		
		JLabel lblPctTime = new JLabel("Pct Time:");
		
		JLabel label_2 = new JLabel("(1-100)");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblName)
						.addComponent(lblSpecialty)
						.addComponent(lblAge)
						.addComponent(lblRank)
						.addComponent(lblSsn)
						.addComponent(labelDept, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPctTime))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(comboBoxDept, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(textFieldSsnFirst, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textFieldSsnMiddle, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(label_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textFieldSsnLast, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))
						.addComponent(textFieldSpecialty, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldName, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(textFieldRank, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
								.addComponent(textFieldAge, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblinteger))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(textFieldPctTime, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(label_2)))
					.addContainerGap(58, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldSsnFirst, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblSsn)
						.addComponent(label)
						.addComponent(textFieldSsnMiddle, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1)
						.addComponent(textFieldSsnLast, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblName))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldAge, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblAge))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldRank, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblRank)
						.addComponent(lblinteger))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSpecialty)
						.addComponent(textFieldSpecialty, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(9)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(comboBoxDept, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(labelDept))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldPctTime, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPctTime)
						.addComponent(label_2))
					.addContainerGap(143, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							addRecord();
							ok = true;
							closeDialog();
						} catch (SQLException e1) {
							JOptionPane.showMessageDialog(null, "Error inserting into database.\n Please check all fields for valid values.",
									"Error In Insert", JOptionPane.ERROR_MESSAGE);
						}
						 catch (NumberFormatException e2) {
								JOptionPane.showMessageDialog(null, "Error inserting into database.\n You appear to have entered invalid integer values in integer fields.",
										"Error In Insert", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						closeDialog();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	/**
	 * Gather the needed data from the dialog, and use the DatabaseConnector to execute an insert statement
	 * to add a professor to the DB
	 * @throws SQLException
	 * @throws NumberFormatException
	 */
	private void addRecord() throws SQLException, NumberFormatException{
		int percentTime = Integer.parseInt(textFieldPctTime.getText());
		if (percentTime >100 || percentTime <1){
			throw new NumberFormatException();
		}
		else{
			Object[] values = new Object[]{
					textFieldSsnFirst.getText()+textFieldSsnMiddle.getText()+textFieldSsnLast.getText(),
					textFieldName.getText(),
					Integer.parseInt(textFieldAge.getText()),
					Integer.parseInt(textFieldRank.getText()),
					textFieldSpecialty.getText()
			};
			conn.executeInsert(DatabaseConnector.INSERT_PROFESSOR_QUERY, values);

			values = new Object[]{
					Integer.parseInt((String)comboBoxDept.getSelectedItem()),
					textFieldSsnFirst.getText()+textFieldSsnMiddle.getText()+textFieldSsnLast.getText(),
					percentTime
			};
			conn.executeInsert(DatabaseConnector.INSERT_DEPT_MEMBER_PROFESSOR, values);
		}
	}


	/**
	 * Populate the departments combobox
	 */
	protected void populateComboBox(){
		try {
			Vector<Vector<Object>> results = conn.executeQuery(DatabaseConnector.DEPT_ID_QUERY, 1);
			
			for(Vector<Object> row:results){
				for(Object value:row){
					comboBoxDept.addItem(value);
				}
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Error querying major IDs from department table.\n ",
					"Error In Query", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * Close the add professor GUI
	 */
	private void closeDialog(){
		this.setVisible(false);
	}

	/**
	 * @return the ok
	 */
	public boolean isOk() {
		return ok;
	}

	/**
	 * @param conn the conn to set
	 */
	public void setConn(DatabaseConnector conn) {
		this.conn = conn;
	}
}
